package org.summerframework.component.security.security.service.controller;

import org.summerframework.component.security.Constants;
import org.summerframework.component.security.LoginData;
import org.summerframework.component.security.controller.BaseController;
import org.summerframework.component.security.core.service.LoginManage;
import org.summerframework.component.security.core.service.LoginService;
import org.summerframework.component.security.enumeration.LoginTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;

/**
 * @author 石超
 * @version v1.0.0
 */
@Api(tags = "用户登录", description = "用户登录")
@Controller
@ConditionalOnMissingClass("org.springframework.security.core.Authentication")
public class LoginController extends BaseController {
    @Resource
    private LoginManage loginManage;

    /**
     * 默认为web登录--web页面form表单
     *
     * @return
     */
    @ApiOperation(value = "登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "json", value = "是否是json 0:界面 1:json", defaultValue = "0", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "loginType", value = "登录类型 0:登录界面 3:用户密码登录", defaultValue = "0", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "style", value = "界面风格", defaultValue = "default", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "redirectURL", value = "登录成功后 json=0时重定向的页面地址", defaultValue = "http://www.gakm.net", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "callback", value = "jsonp", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "json", value = "是否是json 0:界面 1:json", defaultValue = "0", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "from", value = "来源 1:官网  2:安卓  3: iOS", defaultValue = "1", required = false, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "deviceToken", value = "iOS推送", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "username", value = "loginType=3时有效并必传, 用户名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "loginType=3时有效并必传, 密码", dataType = "String", paramType = "query"),
    })
    @RequestMapping(path = "${spring.login.login-path:/}", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView login() {
        return loginManage.login(getContext());
    }

    private String view(Model model, String view) {
        LoginData loginData = getContext().getLoginData();

        if (!view.startsWith("redirect:")) {
            model.addAttribute(RESULT_VAR, getContext().getReusltLoginDate());
            model.addAttribute("callback", getContext().getLoginData().getCallback());
            model.addAttribute("sessionId", getContext().getSession().getId());
            model.addAttribute("sessionKey", getContext().getSession().getAttribute(Constants.SESSION_WECHAR_SESSION_KEY));
        }

        if (loginData.getJson() == 0) {
            //当为html时, 设置为test/html
            getContext().getResponse().addHeader("Content-Type", new MediaType(MediaType.TEXT_HTML, StandardCharsets.UTF_8).toString());
        }

        return view;
    }

    @RequestMapping(value = "${spring.login.loginout:/logout}", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "style", value = "界面风格", defaultValue = "default", required = false, dataType = "String", paramType = "query"),
    })
    @ApiOperation(value = "退出登录")
    public ModelAndView logout(String style) {
        LoginData loginData = LoginService.CURR_LOGIN_DATA.get();

        if (StringUtils.isBlank(style)) {
            loginData.setStyle("logout");
        } else {
            loginData.setStyle(style);
        }

        loginData.setRedirectUrl(getContext().getRequest().getHeader("Referer"));

        return loginManage.login(getContext(), LoginTypeEnum.LOGOUT.getType());
    }
}
